package com.moxiao.yfjh.module.activity.dal.mysql.activityenrollverificate;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.mybatis.core.mapper.BaseMapperX;
import com.moxiao.yfjh.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.moxiao.yfjh.module.activity.controller.admin.activityenrollverificate.vo.ActivityEnrollVerificateExportReqVO;
import com.moxiao.yfjh.module.activity.controller.admin.activityenrollverificate.vo.ActivityEnrollVerificatePageReqVO;
import com.moxiao.yfjh.module.activity.controller.app.vo.AppActivityEnrollVerificatePageReqVO;
import com.moxiao.yfjh.module.activity.controller.app.vo.AppWriteOffVO;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityPackage.ActivityPackageDO;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityenrollverificate.ActivityEnrollVerificateDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;

/**
 * 相亲报名（活动）核销 Mapper
 *
 * @author mx
 */
@Mapper
public interface ActivityEnrollVerificateMapper extends BaseMapperX<ActivityEnrollVerificateDO> {

    default PageResult<ActivityEnrollVerificateDO> selectPage(ActivityEnrollVerificatePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ActivityEnrollVerificateDO>()
                .eqIfPresent(ActivityEnrollVerificateDO::getEnrollId, reqVO.getEnrollId())
                .eqIfPresent(ActivityEnrollVerificateDO::getUserId, reqVO.getUserId())
                .eqIfPresent(ActivityEnrollVerificateDO::getActivityId, reqVO.getActivityId())
                .eqIfPresent(ActivityEnrollVerificateDO::getActivityProductsId, reqVO.getActivityProductsId())
                .eqIfPresent(ActivityEnrollVerificateDO::getVerificationCode, reqVO.getVerificationCode())
                .eqIfPresent(ActivityEnrollVerificateDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ActivityEnrollVerificateDO::getVerificationTime, reqVO.getVerificationTime())
                .betweenIfPresent(ActivityEnrollVerificateDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ActivityEnrollVerificateDO::getId));

    }

    default List<ActivityEnrollVerificateDO> selectList(ActivityEnrollVerificateExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ActivityEnrollVerificateDO>()
                .eqIfPresent(ActivityEnrollVerificateDO::getEnrollId, reqVO.getEnrollId())
                .eqIfPresent(ActivityEnrollVerificateDO::getUserId, reqVO.getUserId())
                .eqIfPresent(ActivityEnrollVerificateDO::getActivityId, reqVO.getActivityId())
                .eqIfPresent(ActivityEnrollVerificateDO::getActivityProductsId, reqVO.getActivityProductsId())
                .eqIfPresent(ActivityEnrollVerificateDO::getVerificationCode, reqVO.getVerificationCode())
                .eqIfPresent(ActivityEnrollVerificateDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ActivityEnrollVerificateDO::getVerificationTime, reqVO.getVerificationTime())
                .betweenIfPresent(ActivityEnrollVerificateDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ActivityEnrollVerificateDO::getId));
    }

    /**
     * 核销码是否重复
     */
    @Select("select count(*) from blind_activity_enroll_verificate where verification_code = #{verificationCode}")
    Integer getVerificationCode(@Param("verificationCode") String verificationCode);

    /**
     * 核销码是否重复
     */
    @Select("select IFNULL(sum(price),0.00)  from blind_activity_enroll_verificate " +
            " where activity_id =#{activityId} and user_id = #{userId} and enroll_id = #{enrollId} " +
            " and status = #{status} ")
    BigDecimal getPrice(@Param("activityId") Long activityId, @Param("userId") Long userId,
                        @Param("status") Byte status, @Param("enrollId") Long enrollId);


    /**
     * app-商家获得已核销列表
     */
    @Select("select * from blind_activity_enroll_verificate where verification_code = #{verificationCode}")
    Integer getVerificationList(@Param("verificationCode") String verificationCode);

    /**
     * App接口：获得商家核销的已列表
     *
     * @return
     */
    default PageResult<AppWriteOffVO> getMerchantVerificationList(AppActivityEnrollVerificatePageReqVO reqVO) {
        return selectJoinPage(reqVO, AppWriteOffVO.class, new MPJLambdaWrapper<ActivityEnrollVerificateDO>()
                .selectAll(ActivityEnrollVerificateDO.class)
                .leftJoin(ActivityPackageDO.class, on -> on
                        .eq(ActivityPackageDO::getActiveId, ActivityEnrollVerificateDO::getActivityId)
                        .eq(ActivityPackageDO::getProductId, ActivityEnrollVerificateDO::getActivityProductsId))
                .selectAs(ActivityPackageDO::getMerchantId, AppWriteOffVO::getMerchantId)
                .eq(reqVO.getMerchantId() != null, ActivityPackageDO::getMerchantId, reqVO.getMerchantId())
                .eq(true, ActivityEnrollVerificateDO::getStatus, 1)
                .orderByDesc(ActivityEnrollVerificateDO::getVerificationTime));
    }

}
